//
//  ViewController.m
//  20_JSONHomework
//
//  Created by 徐昊 on 16/8/10.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"
#import "FindCar.h"
#import "Result.h"
#import "Brandlist.h"
#import "Brand.h"
#import "CustomTableViewCell.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) FindCar *findCar;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"car" ofType:@"json"];
    NSData *jsonData = [NSData dataWithContentsOfFile:jsonPath];
    NSLog(@"%@", jsonData);
    NSError *error = nil;
    NSDictionary *findCarDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    _findCar = [FindCar modelWithDic:findCarDic];
    
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return _findCar.result.brandlist[section].letter;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _findCar.result.brandlist[section].list.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _findCar.result.brandlist.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    Brand *brand = _findCar.result.brandlist[indexPath.section].list[indexPath.row];
    cell.label.text = brand.name;
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
